<template>
  <div class="article-comments">
    <!-- 导航栏 -->
    <van-nav-bar :title="`${currComment.reply_count}条回复`">
      <!-- 关闭按钮 子传父 -->
      <van-icon slot="left" name="cross" @click="closePop" />
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 当前评论 -->
    <van-cell title="当前评论">
      <van-image
        slot="icon"
        round
        width="30"
        height="30"
        style="margin-right: 10px;"
        :src="currComment.aut_photo"
      />
      <span style="color: #466b9d;" slot="title">
        {{ currComment.aut_name }}
      </span>
      <div slot="label">
        <p style="color: #363636;">{{ currComment.content }}</p>
        <p>
          <span style="margin-right: 10px;">{{
            currComment.pubdate | rTime
          }}</span>
          <van-button size="mini" type="default"
            >回复 {{ currComment.reply_count }}</van-button
          >
        </p>
      </div>
      <van-icon slot="right-icon" />
    </van-cell>
    <!-- /当前评论 -->
    <van-divider>全部回复</van-divider>

    <!-- 对当前评论 回复列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(item, index) in list" :key="index">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.pubdate | rTime }}</span>
          </p>
        </div>
      </van-cell>
    </van-list>
    <!-- 回复列表 -->

    <!-- 发布回复 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入回复内容" v-model="msg">
        <van-button slot="button" size="mini" type="info" @click="replyComment"
          >发布</van-button
        >
      </van-field>
    </van-cell-group>
    <!-- /发布回复 -->
  </div>
</template>
<script>
import { comments, pushCom } from "@/api/comment.js";
export default {
  name: "CommentReply",
  props: {
    currComment: {
      type: Object
      //   required: true
    },
    artid: {
      type: String
    }
  },
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null, //用来翻页的  后端传参默认可以不传的可以设置为null
      msg: ""
    };
  },
  methods: {
    closePop() {
      this.$emit("closePop");
    },
    async onLoad() {
      const comid = this.currComment.com_id.toString();
      const type = "c";
      const offset = this.offset;
      try {
        const res = await comments({
          type: type,
          source: comid,
          offset: offset
        });
        if (res.data.data.results.length === 0) {
          this.finished = true;
          return;
        }
        this.list.push(...res.data.data.results);
        this.loading = false;
        this.offset = res.data.data.last_id;
      } catch {
        this.finished = true;
        this.$toast("获取失败");
      }
      // 异步更新数据
      //   setTimeout(() => {
      //     for (let i = 0; i < 10; i++) {
      //       this.list.push(this.list.length + 1);
      //     }
      //     // 加载状态结束
      //     this.loading = false;

      //     // 数据全部加载完成
      //     if (this.list.length >= 40) {
      //       this.finished = true;
      //     }
      //   }, 500);
    },
    async replyComment() {
      //1.获取评论内容
      const msg = this.msg;
      //优化  如果评论内容为空，不执行下面的代码并给出提示
      if (!msg) {
        this.$toast("回复内容不能为空");
        return;
      }
      //2.准备参数
      const comid = this.currComment.com_id;
      const artid = this.artid;
      //3.发送请求
      try {
        //3-1 成功  渲染数据
        const res = await pushCom({
          target: comid,
          content: msg,
          art_id: artid
        });
        this.list.unshift(res.data.data.new_obj);
        this.$toast("回复成功");
        //清空输入框
        this.msg = "";
        // location.reload();//刷新当前页面
      } catch {
        //3-2 失败 给出提示
        this.$toast("回复失败");
      }
    }
  }
};
</script>
<style lang="less" scoped>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
